package com.yanqu.road.dataHelper.repair;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.entity.player.UserInfo;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/14
 */
public abstract class BaseRepair {
    GameServer gameServer;

    public BaseRepair(GameServer gameServer) {
        this.gameServer = gameServer;
    }

    public GameServer getGameServer() {
        return gameServer;
    }



    public Connection getConnection() {
        return this.gameServer.getConnection();
    }

    public Logger getLogger() {
        return this.gameServer.getLogger();
    }

    public abstract void repair(long param1, String param2);


    protected String checkNullToEmpty(String target){
        if(target == null){
            return "";
        }
        return target;
    }

    protected Map<Long, UserInfo> userInfoMap = new ConcurrentHashMap<>();

    protected UserInfo getUserInfo(String userId){
        if(userInfoMap.containsKey(Long.valueOf(userId))){
            return  userInfoMap.get(Long.valueOf(userId));
        }
        UserInfo userInfo = new UserInfo();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select *,FROM_UNIXTIME(registerTime,'%Y年%m月%d %H:%i:%s') as registerTime2," +
                "FROM_UNIXTIME(lastLoginTime,'%Y年%m月%d %H:%i:%s') as lastLoginTime2 from t_u_user WHERE userId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userInfo.setUserId(rs.getLong("userId"));
                userInfo.setNickName(rs.getString("nickName"));
                userInfo.setOriginalServerId(rs.getLong("originalServerId"));
                userInfo.setOpenId(rs.getString("openId"));
                userInfo.setVipExp(rs.getLong("vipExp"));
                userInfo.setRegisterTime(rs.getLong("registerTime"));
                userInfo.setLastLoginTime(rs.getLong("lastLoginTime"));
                userInfo.setSex(rs.getShort("sex"));
                userInfo.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
                userInfo.setTitleId(rs.getInt("titleId"));
                if(userInfoMap.containsKey(userInfo.getUserId())){
                    userInfoMap.put(userInfo.getUserId(),userInfo);
                }
                return userInfo;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return userInfo;
        }
    }

    public Map<String,String> getUserInfo2(String userId){
        Map<String,String> userInfo = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,vipExp,FROM_UNIXTIME(registerTime,'%Y年%m月%d %H:%i:%s') as registerTime," +
                "FROM_UNIXTIME(lastLoginTime,'%Y年%m月%d %H:%i:%s') as lastLoginTime FROM t_u_user WHERE userId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userInfo.put("userId",rs.getString("userId"));
                userInfo.put("vipExp",rs.getString("vipExp"));
                userInfo.put("registerTime",rs.getString("registerTime"));
                userInfo.put("lastLoginTime",rs.getString("lastLoginTime"));
                return userInfo;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return userInfo;
    }
}
